﻿using AgencyManagerSystem.Model;
using System.Collections.Generic;
using System.Linq;

namespace AgencyManagerSystem.DataFetcher
{
    public class DepartmentFetcher : ErpFetcher<DepartmentModel>
    {
        public DepartmentFetcher() : base()
        {

        }
        public DepartmentModel GetByCode(string code)
        {
            Condition[] conditions = new Condition[] { new Condition() { Key = "Code", Op = "=", Value = code } };
            var list = List(conditions);
            if (list.Count > 0)
                return list.First();
            else
                return null;
        }

        public IList<DepartmentModel> ListByParent(int parent)
        {
            Condition[] conditions = new Condition[] { new Condition() { Key = "Parent", Op = "=", Value = parent } };
            var list = List(conditions);
            return list;
        }

        public DepartmentModel GetRoot()
        {
            Condition[] conditions = new Condition[] { new Condition() { Key = "Parent", Op = "=", Value = 0 } };
            var list = List(conditions);
            if (list.Count == 0)
                return null;
            return list.First();
        }

        public IList<DepartmentModel> SearchByName(string name)
        {
            Condition[] conditions = new Condition[] { new Condition() { Key = "Name", Op = "like", Value = "%" + name + "%" } };
            var list = List(conditions);
            return list;
        }
    }
}